package com.hl.card.credit.controller;
import com.github.pagehelper.PageInfo;
import com.hl.card.credit.domain.CardStatus;
import com.hl.card.credit.domain.CreditCardOrder;
import com.hl.card.credit.service.CreditCardOrderService;
import com.hl.card.credit.service.PingAnCreditCardService;
import com.hl.card.credit.utill.PingAnTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("back")
public class CreditCardOrderBackController {
    private static final Logger logger = LoggerFactory.getLogger(CreditCardOrderController.class);
    @Autowired
    private CreditCardOrderService creditCardOrderService;

    @Autowired
    private PingAnCreditCardService pingAnCreditCardService;
    
    /**
     * 查询信用卡状态
     * @param orderCode
     * @return
     */
    @RequestMapping(value ="queryOrder")
    public Object queryOrder(String orderCode,String token){
        Map<String,Object> map=new HashMap<>();
       /* if (!creditCardOrderService.checkToken(token)) {
            map.put("msg", "无效token");
            return map;
        }*/
        try {
            CreditCardOrder cco= creditCardOrderService.getSatusByOrderCode(orderCode);
            if(null==cco){
                map.put("code","2");
                map.put("msg","订单不存在");
                map.put("data",cco);
                return map;
            }
            String orederId=cco.getOrderid();
            CardStatus cardStatus= pingAnCreditCardService.getPortStatus(orederId);
            if(cardStatus==null){
                map.put("code","3");
                map.put("msg","未完成信用卡申请信息填写，无效订单");
                map.put("data",cco);
                return map;
            }
            if(cardStatus.getActivated()!=null){
                cco.setActivated(cardStatus.getActivated());
            }
            if(cardStatus.getApplicationStatus()!=null){
                cco.setApplicationstatus(cardStatus.getApplicationStatus());
            }
            if(cardStatus.getApplyCompleted()!=null){
                cco.setApplycompleted(cardStatus.getApplyCompleted());
            }
            if(cardStatus.getBarCode()!=null){
                cco.setBarcode(cardStatus.getBarCode());
            }
            if(cardStatus.getFirstUsed()!=null){
                cco.setFirstused(cardStatus.getFirstUsed());
            }
            String currentStatus= (String) PingAnTool.getOrderStatus(cco).get("currentStatus");
            cco.setCurrentstatus(currentStatus);
            creditCardOrderService.update(cco);
            map.put("code","1");
            map.put("msg","审核中");
            map.put("data",cco);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("code","0");
            map.put("msg","审核未通过");
            map.put("data","");
        }
        return map;
    }
    /**
     * 查询用户所有订单列表
     * @param phone
     * @return
     */
    @RequestMapping(value = "queryAllOrder")
    public Object queryAllOrder(@RequestParam("phone") String phone,@RequestParam(name="pageNum",required =false) Integer page,@RequestParam(name="pageSize",required =false )  Integer pageSize,String token){
        Map<String,Object> map=new HashMap<>();
      /*  if (!creditCardOrderService.checkToken(token)) {
            map.put("msg", "无效token");
            return map;
        }*/
        try {
            PageInfo<CreditCardOrder> list=creditCardOrderService.list(phone,page,pageSize);
            map.put("code","1");
            map.put("msg","查询成功并显示所有订单列表");
            map.put("data",list);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("code","0");
            map.put("msg","你还没有注册！请注册用户");
            map.put("data","");
        }
        return map;
    }
    /**
     * 查询订单进度
     * @param orderId
     * @return
     */
    @GetMapping(value = "queryOrderPogress")
    public Object queryOrderrPogress(String orderId,String token){
        Map<String,Object> map=new HashMap<>();
      /*  if (!creditCardOrderService.checkToken(token)) {
            map.put("msg", "无效token");
            return map;
        }*/
        CardStatus cardStatus=null;
        CreditCardOrder cco=null;
        try {
        	cardStatus = pingAnCreditCardService.getPortStatus(orderId);        	
        	cco= creditCardOrderService.getSatusByOrderCode(orderId);
        	if(null!=cardStatus&&null!=cco){
                if(cardStatus.getActivated()!=null){
                    cco.setActivated(cardStatus.getActivated());
                }
                if(cardStatus.getApplicationStatus()!=null){
                    cco.setApplicationstatus(cardStatus.getApplicationStatus());
                }
                if(cardStatus.getApplyCompleted()!=null){
                    cco.setApplycompleted(cardStatus.getApplyCompleted());
                }
                if(cardStatus.getBarCode()!=null){
                    cco.setBarcode(cardStatus.getBarCode());
                }
                if(cardStatus.getFirstUsed()!=null){
                    cco.setFirstused(cardStatus.getFirstUsed());
                }
            }
            String currentStatus= (String) PingAnTool.getOrderStatus(cco).get("currentStatus");
            cco.setCurrentstatus(currentStatus);
            creditCardOrderService.update(cco);
            map.put("code","1");
            map.put("msg","审核中");
            map.put("data",cco);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("code","0");
            map.put("msg","审核未通过");
            map.put("data","");
        }
        return map;
    }
    @GetMapping("listAllOrders")
    public  Object listAllOrders(@RequestParam(name = "condition",required = false) String condition){
        List<CreditCardOrder> list=creditCardOrderService.listAll(condition);
        return list;
    }
    /**
     * 分页查询
     * @return
     */
    @GetMapping("listOrders")
    public  Object listOrders( Integer offset,Integer size){
        Map map=creditCardOrderService.listAll( offset,size);
        return map;
    }
    @GetMapping("selectById")
    public Object selectById(Long id){
      return   creditCardOrderService.findById(id);
    }
}
